<?php

class AdminsController extends AppController {

    var $name = 'Admins';
    var $helpers = array('Html', 'Javascript');

    //var $components = array('Auth');


    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->userModel = 'admin';
        $this->Auth->fields = array('username' => 'user', 'password' => 'mot-passe');
         $this->Auth->allow('login', 'nouveau','initDB','buildAcl');
//        $this->Auth->allowedActions = array('*');

        $this->Auth->loginAction = array('controller' => 'admins', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
        $this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
    }

    function login() {
        if (!$this->Auth->user()) {
            if (!empty($this->data)) {
                if ($this->Auth->user()) {
                    $this->Auth->redirect(array('controller' => 'pages', 'action' => 'display', 'home'));
                } else {
                    $this->set('auth_msg', 'Essaye encore');
                }
            }
        } else {
            $this->Session->setFlash('Vous etes déjà enregistre');
            $this->redirect(array('controller' => 'commands', 'action' => 'afficher'));
        }
    }

    function logout() {
        $this->redirect($this->Auth->logout());
    }

    function index() {
        $this->Admin->recursive = 0;
        $this->set('admins', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid admin', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('admin', $this->Admin->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->Admin->create();
            if ($this->Admin->save($this->data)) {
                $this->Session->setFlash(__('The admin has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The admin could not be saved. Please, try again.', true));
            }
        }
        $groups = $this->Admin->Group->find('list');
        $this->set(compact('groups'));
    }

    function edit($id = null) {
        
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid admin', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
                debug($this->data['Admin']);
                debug( $this->Auth->password($this->data['Admin']['mot-passe1']));
                debug( $this->Auth->password($this->data['Admin']['mot-passe2']));
                if($this->data['Admin']['mot-passe3']==$this->Auth->password($this->data['Admin']['mot-passe1'])){
                $this->data['Admin']['mot-passe']=$this->Auth->password($this->data['Admin']['mot-passe2']);
            if ($this->Admin->save($this->data)) {
                

                // Verificar si sus permisos de grupo han cambiado
                $oldgroupid = $this->Admin->field('group_id');
                if ($oldgroupid !== $this->data['Admin']['group_id']) {
                    $aro = & $this->Acl->Aro;
                    $user = $aro->findByForeignKeyAndModel($this->data['Admin']['id'], 'Admin');
                    $group = $aro->findByForeignKeyAndModel($this->data['Admin']['group_id'], 'Group');

                    // Guardar en la tabla ARO
                    $aro->id = $user['Aro']['id'];
                    $aro->save(array('parent_id' => $group['Aro']['id']));
                }



                $this->Session->setFlash(__('The admin has been saved', true));
                $this->redirect(array('action' => 'index'));
                
                    
                
            } else {
                $this->Session->setFlash(__('The admin could not be saved. Please, try again.', true));
            }
        }else{
             $this->Session->setFlash(__('Mot-passe Incorrect.', true));
        }
        }
        if (empty($this->data)) {
            $this->data = $this->Admin->read(null, $id);
        }
        $groups = $this->Admin->Group->find('list');
        $this->set(compact('groups'));
    
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for admin', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Admin->delete($id)) {
            $this->Session->setFlash(__('Admin deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Admin was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }






//    function initDB() {
//    $group =& $this->Admin->Group;
//    //Permite a los administradores hacer todo
//    $group->id = 2;
//    $this->Acl->allow($group, 'controllers');
////    permite a los editores postear y accesar los widgets
////    $group->id = 2;
////    $this->Acl->deny($group, 'controllers');
////    $this->Acl->allow($group, 'controllers/Posts');
////    $this->Acl->allow($group, 'controllers/Widgets');
////    permite a los usuarios añadir y editar posts y widgets
////    $group->id = 2;
////    $this->Acl->allow($group, 'controllers');
////    $this->Acl->allow($group, 'controllers/Posts/add');
////    $this->Acl->allow($group, 'controllers/Posts/edit');
////    $this->Acl->allow($group, 'controllers/Widgets/add');
////    $this->Acl->allow($group, 'controllers/Widgets/edit');
//}









    /**
 * Reconstruye el Acl basado en los controladores actuales de la aplicación.
 *
 * @return void
 */
//    function buildAcl() {
//        $log = array();
//
//        $aco =& $this->Acl->Aco;
//        $root = $aco->node('controllers');
//        if (!$root) {
//            $aco->create(array('parent_id' => null, 'model' => null, 'alias' => 'controllers'));
//            $root = $aco->save();
//            $root['Aco']['id'] = $aco->id;
//            $log[] = 'Creado el nodo Aco para los controladores';
//        } else {
//            $root = $root[0];
//        }
//
//        App::import('Core', 'File');
//        $Controllers = Configure::listObjects('controller');
//        $appIndex = array_search('App', $Controllers);
//        if ($appIndex !== false ) {
//            unset($Controllers[$appIndex]);
//        }
//        $baseMethods = get_class_methods('Controller');
//        $baseMethods[] = 'buildAcl';
//
//        // miramos en cada controlador en app/controllers
//        foreach ($Controllers as $ctrlName) {
//            App::import('Controller', $ctrlName);
//            $ctrlclass = $ctrlName . 'Controller';
//            $methods = get_class_methods($ctrlclass);
//
//            //buscar / crear nodo de controlador
//            $controllerNode = $aco->node('controllers/'.$ctrlName);
//            if (!$controllerNode) {
//                $aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $ctrlName));
//                $controllerNode = $aco->save();
//                $controllerNode['Aco']['id'] = $aco->id;
//                $log[] = 'Creado el nodo Aco del controlador '.$ctrlName;
//            } else {
//                $controllerNode = $controllerNode[0];
//            }
//
//            //Limpieza de los metodos, para eliminar aquellos en el controlador
//            //y en las acciones privadas
//            foreach ($methods as $k => $method) {
//                if (strpos($method, '_', 0) === 0) {
//                    unset($methods[$k]);
//                    continue;
//                }
//                if (in_array($method, $baseMethods)) {
//                    unset($methods[$k]);
//                    continue;
//                }
//                $methodNode = $aco->node('controllers/'.$ctrlName.'/'.$method);
//                if (!$methodNode) {
//                    $aco->create(array('parent_id' => $controllerNode['Aco']['id'], 'model' => null, 'alias' => $method));
//                    $methodNode = $aco->save();
//                    $log[] = 'Creado el nodo Aco para '. $method;
//                }
//            }
//        }
//        debug($log);
//    }


}

?>